package cn.com.wangluotongxin.mall.service.impl;

import cn.com.wangluotongxin.mall.model.MallQuickQuery;
import cn.com.wangluotongxin.mall.mapper.MallQuickQueryMapper;
import cn.com.wangluotongxin.mall.rabbitmq.pojo.QuickQueryPojo;
import cn.com.wangluotongxin.mall.service.IMallQuickQueryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网络通信-商城APP-快捷查询 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-10-21
 */
@Service
public class MallQuickQueryServiceImpl extends ServiceImpl<MallQuickQueryMapper, MallQuickQuery> implements IMallQuickQueryService {

    @Override
    public void quickQuerySync(QuickQueryPojo pojo) {
        MallQuickQuery quickQuery = this.loadInfo(pojo);
        if(quickQuery.getId() != null) {
            if(pojo.getLoginUser() != null) {
                quickQuery.setModifyUser(pojo.getLoginUser().getId());
                quickQuery.setModifyName(pojo.getLoginUser().getUsername());
            }
            this.updateById(quickQuery);
        } else {
            if(pojo.getLoginUser() != null) {
                quickQuery.setCreateName(pojo.getLoginUser().getUsername());
                quickQuery.setCreateUser(pojo.getLoginUser().getId());
                quickQuery.setModifyUser(pojo.getLoginUser().getId());
                quickQuery.setModifyName(pojo.getLoginUser().getUsername());
            }
           this.save(quickQuery);
        }
    }

    @Override
    public MallQuickQuery getBySource(Integer sourceId) {
        return this.getOne(new LambdaQueryWrapper<MallQuickQuery>()
                .eq(MallQuickQuery::getSourceId, sourceId)
                .last("limit 0,1"));
    }

    private MallQuickQuery loadInfo(QuickQueryPojo pojo) {
        MallQuickQuery quickQuery = new MallQuickQuery();

        MallQuickQuery quick = this.getBySource(pojo.getSourceId());
        if(quick != null) {
            quickQuery.setId(quick.getId());
        }
        quickQuery.setSourceId(pojo.getSourceId());
        quickQuery.setQuickName(pojo.getQuickName());
        quickQuery.setQuickRemark(pojo.getQuickRemark());
        quickQuery.setQuickSort(pojo.getQuickSort());
        quickQuery.setIsEnabled(pojo.getIsEnabled());
        return quickQuery;
    }
}
